#include "videosourcesetting.h"
#include <QSettings>
#include "alghelper.h"

VideoSourceSetting::VideoSourceSetting()
{

}

int VideoSourceSetting::readVSIniFile()
{
    QSettings settings(VIDEO_SOURCE_SETTING_FILE, QSettings::IniFormat);
    if(settings.status() == QSettings::NoError)
    {
        QStringList groups = settings.childGroups();
        listVideoSource.clear();
        foreach (QString group, groups) {
            IVS::VideoSource videoSource;
            settings.beginGroup(group);
            videoSource.sourceName = group;
            videoSource.sourceType = (IVS::eVideoSourceType) settings.value("source_type", IVS::LocalFile).toInt();
            videoSource.source = settings.value("source", "").toString();
            videoSource.ROI = algHelper::cvtText2PointList(settings.value("ROI", "{}").toString());
            settings.endGroup();

            listVideoSource.append(videoSource);
        }

        return 0;
    }else
        return -1; // access error or malformat
}

int VideoSourceSetting::writeVSIniFile()
{
    QSettings settings(VIDEO_SOURCE_SETTING_FILE, QSettings::IniFormat);
    if(settings.status() == QSettings::NoError)
        for(int i =0; i < listVideoSource.count(); i++ )
        {
            settings.beginGroup(listVideoSource[i].sourceName);
            settings.setValue("source_type", listVideoSource[i].sourceType);
            settings.setValue("source", listVideoSource[i].source);
            settings.setValue("ROI", algHelper::cvtPointList2Text(listVideoSource[i].ROI));
            settings.endGroup();
        }
    else
        return -1;

    return 0;
}

bool VideoSourceSetting::addVideoSource(IVS::VideoSource _videoSource)
{
    foreach (IVS::VideoSource videoSource, listVideoSource) {
        if(videoSource.sourceName.compare(_videoSource.sourceName) == 0)
            return false;
    }

    listVideoSource.append(_videoSource);
    return true;
}
